<template>
  <div class="table">
    <div class="container">
      <div class="handle-box">
        <el-form ref="queryFormData" :model="queryFormData" inline size="small">
          <el-form-item label="注册日期">
            <el-date-picker v-model="rangeDateValue" align="center" end-placeholder="结束日期" range-separator="-"
                            start-placeholder="开始日期" type="daterange" unlink-panels>
            </el-date-picker>
          </el-form-item>
          <el-button size="small" icon="el-icon-search" @click="search">搜索</el-button>
        </el-form>
      </div>
      <el-table v-loading="loading" :data="tableData" size="mini" style="width: 100%">
        <el-table-column label="头像" width="110" align="center">
          <template slot-scope="{row}">
            <div class="user-img">
              <img :src="row.pic" style="width: 100%; border-radius: 50%;"/>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="昵称" align="center"/>
        <el-table-column prop="genderDesc" label="性别" width="80" align="center"/>
        <el-table-column prop="phone" label="手机号" align="center"/>
        <el-table-column prop="bir" label="生日" align="center"/>
        <el-table-column prop="pwd" label="密码" align="center"/>
        <el-table-column prop="email" label="邮箱" align="center"/>
        <el-table-column prop="createTime" label="注册时间" align="center"/>
        <el-table-column align="center" label="喜爱歌曲" prop="loveSong" width="90">
          <template slot-scope="{row}">
            <el-button size="mini" :disabled="row.dataFlag === 1" @click="loveSong(row.id)"
                       icon="el-icon-headset">{{ row.loveSong }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column align="center" label="关注歌手" prop="followSinger" width="90">
          <template slot-scope="{row}">
            <el-button size="mini" :disabled="row.dataFlag === 1" @click="followSinger(row.id)"
                       icon="el-icon-mic">{{ row.followSinger }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column align="center" label="收藏歌单" prop="collectSongList" width="90">
          <template slot-scope="{row}">
            <el-button size="mini" :disabled="row.dataFlag === 1" @click="collectSongList(row.id)"
                       icon="el-icon-star-off">{{ row.collectSongList }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column align="center" label="自建歌单" prop="createSongList" width="90">
          <template slot-scope="{row}">
            <el-button size="mini" :disabled="row.dataFlag === 1" @click="createSongList(row.id)"
                       icon="el-icon-document">{{ row.createSongList }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" prop="operation" width="140">
          <template slot-scope="{row}">
            <el-switch v-model="row.dataFlag" :active-value="0" :inactive-value="1" active-text="生效"
                       inactive-text="失效" @change="change(row.id)"/>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination @current-change="handleCurrentChange" background layout="prev, pager, next, total"
                       :current-page="pageNum" :page-size="pageSize" :total="total"/>
      </div>
    </div>
  </div>
</template>

<script>
import {listConsumer, updateConsumerDataFlag} from '../api/index';

export default {
  data() {
    return {
      tableData: [],
      pageNum: 1,
      pageSize: 10,
      total: 0,
      loading: true,
      queryFormData: {},
      rangeDateValue: '',
    }
  },
  mounted() {
    this.getData();
  },
  watch: {
    rangeDateValue: {
      handler: function (val) {
        if (val && val.length === 2) {
          this.queryFormData.createStartTime = this.dayjs(val[0]).format("YYYY-MM-DD 00:00:00");
          this.queryFormData.createEndTime = this.dayjs(val[1]).format("YYYY-MM-DD 23:59:59");
        } else {
          this.queryFormData.createStartTime = '';
          this.queryFormData.createEndTime = '';
        }
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
    getData() {
      this.loading = true;
      const {queryFormData, pageNum, pageSize} = this;
      listConsumer(queryFormData, pageNum, pageSize).then(res => {
        this.loading = false;
        const {data, total, success} = res;
        if (!success) {
          this.$message.error('查询失败');
          this.tableData = [];
          return;
        }
        this.tableData = data.map(e => {
          return {
            ...e,
            toggle: false
          }
        });
        this.total = total;
      })
    },
    search() {
      this.getData();
    },
    handleCurrentChange(currentPageNum) {
      this.pageNum = currentPageNum;
      this.getData();
    },
    change(id) {
      updateConsumerDataFlag(id).then(res => {
        const {success} = res;
        if (success) {
          this.$message.success("操作成功");
          this.getData();
        } else {
          this.$message.error("操作失败");
        }
      });
    },
    loveSong(id) {
      this.$router.push({path: "/LoveSong", query: {userId: id}});
    },
    collectSongList(id) {
      this.$router.push({path: "/CollectSongList", query: {userId: id}});
    },
    createSongList(id) {
      this.$router.push({path: "/SongList", query: {userId: id}});
    },
    followSinger(id) {
      this.$router.push({path: "/FollowSinger", query: {userId: id}});
    }

  }
}

</script>

<style scoped>
.user-img {
  width: 100%;
  height: 90px;
  border-radius: 5px;
  margin-top: 5px;
  margin-bottom: 5px;
  overflow: hidden;
}
</style>